function clickContent(index){
	contentNow = index;
	$(".active").removeClass("active");
	$("#c_"+index).addClass("active");
	$(".content").slideUp();
	$("#content_"+index).slideDown();
	switch(index){
		case 1: readyContent_1(1); break;
		case 2: readyContent_2(1); break;
		case 3: readyContent_3(1); break;
	}
}

var bandIds = "";
var taskIds = "";
var superMarketIds = "";
var employeeIds = "";
var contentNow = 1;
var employees = null;

var marketArray = new Array();
var marketEmpArray = new Array();
var brandArray = new Array();
var brandStrArray = new Array();

function saveContent(type){
	switch(type){
	case 1: 
		taskIds = "";
		$('[name="taskRadio"]:radio').each(function() {
			if(this.checked){
				taskIds = $(this).val();
			}
		});
		clickContent(2);
		break;
	case 2: 
		bandIds = "";
		$("input[name='bandCheckBox']:checkbox:checked").each(function(){ 
			if($(this).attr("checked")){
				bandIds += $(this).val()+",";
			}
		});
		break;
	case 3: 
		superMarketIds = "";
		$("input[name='marketRadio']:checkbox:checked").each(function(){ 
			if($(this).attr("checked")){
				superMarketIds += $(this).val()+",";
			}
		});
		employees = "";
		$("input[name='employeeBox']:checkbox:checked").each(function(){ 
			if($(this).attr("checked")){
				employees += $(this).val()+",";
			}
		});
		save();
		break;
	case 4:
		clickContent(4);
		break;
	}
	/*case 4: 
		employeeIds = "";
		$('[name="employeeRadio"]:radio').each(function() {
			if(this.checked){
				employeeIds = $(this).val();
			}
		});
		var str = '品牌ID：'+bandIds+'  任务：'+taskIds+' 门店：'+superMarketIds+' 业务员：'+employeeIds;
		alert(str);
		break;
	}*/
}

function save(){
	var tmp2=root+"/admin/task-product-employee!add";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"task.id":taskIds, "brandIds": bandIds, "superMarketIds": superMarketIds, "employeeIds":employees},
		dataType:"json",
		success: function(json){
			closeAdd();
			if(json.code=="y"){
				findList(1);
			}
		}
	});
}

function readyContent_1(pageNow){
	var tmp2=root+"/admin/task!findTaskByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "page.numberPerPage": 40},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<li><label><input type="radio" name="taskRadio" value="'+json.pageData[i].id+'" />';
				htmlStr += '&nbsp;&nbsp;&nbsp;'+json.pageData[i].content+'</lable></li>';
			}
			htmlStr += '<li style="text-align: left; cursor: pointer; width: 85%; border-bottom:none;">';
			htmlStr += '<span onclick="readyContent_1('+1+')">首页</span>';
			if(json.curPageNum>1){
				htmlStr += '<span onclick="readyContent_1('+(json.curPageNum-1)+')">上一页</span>';
			}else{
				htmlStr += '<span>上一页</span>';
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				htmlStr += '<span onclick="readyContent_1('+(json.curPageNum+1)+')">下一页</span>';
			}else{
				htmlStr += '<span>下一页</span>';
			}
			htmlStr += '<span onclick="readyContent_1('+json.totalPageNum+')">尾页</span>';
			htmlStr += '<span>('+json.curPageNum+'/'+json.totalPageNum+')</span>';
			htmlStr += '</li>';
			htmlStr += '<li style="text-align: right; cursor: pointer; width: 15%;">';
			htmlStr += '<span onclick="saveContent(1)">下一步</span></li>';
			$("#taskUl").html(htmlStr);
		}
	});
}

function readyContent_2(pageNow){
	var tmp2=root+"/admin/brand!findBrandByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<li><label><input id="bandRadio_'+i+'" type="checkBox" name="bandCheckBox" value="'+json.pageData[i].id+'" onclick="showMarket('+i+')"';
				for(var j=0; j<brandArray.length; j++){
					if(brandArray[j] == json.pageData[i].id){
						htmlStr += ' checked="checked" ';
					}
				}
				htmlStr += ' />';
				htmlStr += '&nbsp;&nbsp;&nbsp;'+json.pageData[i].name+'</label></li>';
			}
			htmlStr += '<li style="text-align: left; cursor: pointer; width: 70%; border-bottom:none;">';
			htmlStr += '<span onclick="readyContent_2('+1+')">首页</span>';
			if(json.curPageNum>1){
				htmlStr += '<span onclick="readyContent_2('+(json.curPageNum-1)+')">上一页</span>';
			}else{
				htmlStr += '<span>上一页</span>';
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				htmlStr += '<span onclick="readyContent_2('+(json.curPageNum+1)+')">下一页</span>';
			}else{
				htmlStr += '<span>下一页</span>';
			}
			
			htmlStr += '<span onclick="readyContent_2('+json.totalPageNum+')">尾页</span>';
			htmlStr += '<span>('+json.curPageNum+'/'+json.totalPageNum+')</span>';
			htmlStr += '</li>';
			htmlStr += '<li style="text-align: right; cursor: pointer; width: 30%;">';
			htmlStr += '<span onclick="clickContent(1)">上一步</span><span onclick="saveContent(4)">下一步</span></li>';
			$("#bandUl").html(htmlStr);
		}
	});
}

var brandid = "";
var brandIndex = 0;
function showMarket(index){
	if($("#bandRadio_"+index).attr("checked")){
		brandid = $("#bandRadio_"+index).val();
		brandIndex = index;
		brandArray.push($("#bandRadio_"+index).val());
		clickContent(3);
	}else{
		
	}
}

function closeMarket(){
	//组合字符串
	//格式：  品牌；门店,业务员;门店,业务员;-品牌：门店,业务员;门店,业务员;
	for(var i=0; i<brandArray.length; i++){
		if(brandArray[i]==brandid){
			var str = brandid+";";
			for(var j=0; j<marketEmpArray.length; j++){
				str += marketEmpArray[j]+";";
			}
			brandStrArray[i] = str;
		}
	}
	$(".active").removeClass("active");
	$("#c_2").addClass("active");
	$(".content").slideUp();
	$("#content_2").slideDown();
}

var employeeStr = "";
var marketid = "";
function readyContent_3(pageNow){
	marketArray = new Array();
	marketEmpArray = new Array();
	//empArray = new Array();
	if(employeeStr==""){
		findEmployeeAll();
	}
	var tmp2=root+"/admin/super-market!findListByBrand";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "brand.id":brandid, "page.numberPerPage": 10},
		dataType:"json",
		success: function(json){
			var htmlStr = "";
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<li style="width: 100%;"><label><input id="mBox'+i+'" type="checkBox" name="marketRadio" value="'+json.pageData[i].id+'" onclick="showEmployeeSelect('+i+')" />';
				htmlStr += '&nbsp;&nbsp;&nbsp;'+json.pageData[i].company.name+' - '+json.pageData[i].name+'</label>';
				htmlStr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				htmlStr += '<select style="display:none;" id="superM_employee_'+i+'" name="" onchange="employeeSelectChange('+i+')" style="width: 120px; border: 1px solid #666; font-size: 12px;">';
				htmlStr += employeeStr+'</select><input id="mEmpl'+i+'" style="display:none;" type="checkBox" name="employeeBox" value="" /></li>';
			}
			htmlStr += '<li style="text-align: left; cursor: pointer; width: 80%; border-bottom:none;">';
			htmlStr += '<span onclick="readyContent_3('+1+')">首页</span>';
			if(json.curPageNum>1){
				htmlStr += '<span onclick="readyContent_3('+(json.curPageNum-1)+')">上一页</span>';
			}else{
				htmlStr += '<span>上一页</span>';
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				htmlStr += '<span onclick="readyContent_3('+(json.curPageNum+1)+')">下一页</span>';
			}else{
				htmlStr += '<span>下一页</span>';
			}
			
			htmlStr += '<span onclick="readyContent_3('+json.totalPageNum+')">尾页</span>';
			htmlStr += '<span>('+json.curPageNum+'/'+json.totalPageNum+')</span>';
			htmlStr += '</li>';
			htmlStr += '<li style="text-align: right; cursor: pointer; width: 20%;">';
			htmlStr += '<span onclick="clickContent(2)">上一步</span><span onclick="closeMarket()">确定</span></li>';
			$("#superMarketUl").html(htmlStr);
		}
	});
}

function showEmployeeSelect(index){
	if($("#mBox"+index).attr("checked")=="checked"){
		$("#superM_employee_"+index).show();
		$("#mEmpl"+index).attr("checked","true");
		$("#mEmpl"+index).val($("#superM_employee_"+index).val());
		
		marketArray.push($("#mBox"+index).val());
		marketEmpArray.push($("#mBox"+index).val()+","+$("#superM_employee_"+index).val());
	}else{
		$("#mEmpl"+index).removeAttr("checked");
		$("#superM_employee_"+index).hide();
	}
}

function employeeSelectChange(index){
	for(var i=0; i<marketArray.length; i++){
		var str = $("#mBox"+index).val()+","+$("#superM_employee_"+index).val();
		if(marketArray[i] == $("#mBox"+index).val()){
			marketEmpArray[i] = str;
		}
	}
	$("#mEmpl"+index).val($("#superM_employee_"+index).val());
}

function findEmployeeAll(){
	var tmp=root+"/admin/employee!getAll";
	$.ajax({
		url: tmp,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var html = "";
			for(var i=0; i<json.length; i++){
				html += '<option value="'+json[i].id+'">'+json[i].userName+'</option>';
				employeeStr += '<option value="'+json[i].id+'">'+json[i].userName+'</option>';
			}
			$("#emplSelect").append(html);
		}
	});
}
